const base_url = "http://120.55.194.140:8080/funong"
const request = (url,method = 'GET',data={}) => {
	uni.showLoading({
		title: '加载中',
		mask: true,
		icon: 'loading'
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: base_url + url,
			method,
			data,
			header: {
				Authorization: uni.getStorageSync('token')
			},
			success: (res) => {
				uni.hideLoading()
				if (res.data.code == 401) {
					uni.navigateTo({
						url: '/pages/login_register/login_register'
					})
					uni.showToast({
						title: "您还未登陆 ，请登录",
						icon: 'none',
						duration: 2000
					})
				} else if (res.data.code == 200) {

				} else {
					setTimeout(() => {
						uni.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 2000
						})
					}, 0)
				}
				resolve(res.data)
			},
			fail: (error) => {
				uni.hideLoading()
				if (res.data.code == 401) {
					uni.navigateTo({
						url: '/pages/login_register/login_register'
					})
					uni.showToast({
						title: "您还未登陆 ，请登录",
						icon: 'none',
						duration: 2000
					})
				}else{
					setTimeout(() => {
					uni.showToast({
						title: '请求异常',
						icon: 'none',
						duration: 1500
					})
				}, 500)
				}
				reject(error)
				// console.log(error);
			},
			complete() {
				uni.hideLoading()
			}
		})
	})
}

const doPost = async(url,data)=>{
	return await request(url,"POST",data)
}
const doPut = async(url,data)=>{
	return await request(url,"PUT",data)
}
const doDelete = async(url,data)=>{
	return await request(url,"DELETE",data)
}
const doGet = async(url,data)=>{
	return await request(url,"GET",data)
}

//Your code here
async function login(username,password){
	const data = await request('/login',"POST",{
		username,password
	})
	uni.setStorageSync("token",data.token)
	return data
}
// ,phone,email
async function register(username,password,phone,email){
	const data=await doPost("/register",{
		username,password,phone,email
	})	
}
async function userinfos(username,phone){
	const data =await doGet('/userList','GET',{
		username,phone
	})

}
async function getGoodsByCart(uid,page,row){
	return await request("/cart/getGood",undefined,{uid,page,row});
}

const $req = {
	doGet,
	doPost,
	doDelete,
	doPut,
	login,
	register,
	getGoodsByCart
}
export default $req